package com.bustle.commons;

public interface MapConverter<SourceType, DestinationType> extends Converter<SourceType, DestinationType>, Mapper<SourceType, DestinationType>
{
  abstract public class AbstractMapConverter<SourceType, DestinationType> implements MapConverter<SourceType, DestinationType>
  {
    abstract protected DestinationType build();
    abstract protected void doMapping(SourceType aSource, DestinationType aDestination);

    @Override
    public DestinationType convert(SourceType aSource)
    {
      if (aSource != null)
      {
        DestinationType destination = build();
        map(aSource, destination);
        return destination;
      }
      return null;
    }

    @Override
    public DestinationType map(SourceType aSource, DestinationType aDestination)
    {
      DestinationType destination = aDestination;
      if (destination == null)
      {
        destination = build();
      }
      doMapping(aSource, destination);
      return destination;
    }
  }
}